import request from '@/utils/request.js';
import {download, toFormData} from '@/utils/common.js';

/**
 * 分页查询学生基础信息
 */
export async function pageList(params) {
  const res = await request.get('/admin/student/baseinfo/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加学生基础信息
 */
export async function addStudent(data) {
  const res = await request.post('/admin/student/baseinfo', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改学生基础信息
 */
export async function editStudent(data) {
  const res = await request.put('/admin/student/baseinfo', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取学生基础信息
 * @returns {Promise<any>}
 */
export async function selectStduentInfo(id) {
  const res = await request.get('/admin/student/baseinfo/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 删除学生基础信息
 */
// export async function removeStudent(id) {
//   const res = await request.delete('/admin/student/baseinfo/' + id);
//   if (res.data.code === 200) {
//     return res.data.msg;
//   }
//   return Promise.reject(new Error(res.data.msg));
// }

/**
 * 批量删除学生基础信息
 */
export async function removeStudents(ids) {
  const res = await request.delete('/admin/student/baseinfo/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出学生基础信息
 */
export async function exportBases(params) {
  const res = await request({
    url: '/admin/student/baseinfo/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  download(res.data, `base_${Date.now()}.xlsx`);
}

/**
 * 初始化系别年级专业
 * @returns {Promise<any>}
 */
export async function initSchoolDegree() {
  const res = await request.get('/admin/school/degree/init');
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 下载学生导入模板
 * @returns {Promise<void>}
 */
export async function downloadTemplate() {
  const res = await request({
    url: '/admin/student/baseinfo/downloadTemplate',
    method: 'POST',
    responseType: 'blob'
  });
  download(res.data, `place_info_template_${Date.now()}.xlsx`);
}


/**
 * 导入学生基础数据
 */
export async function importStudentBase(fileName) {
  let data = {};
  data.fileName = fileName;
  //`timeout` 指定请求超时的毫秒数(0 表示无超时时间)
  const res = await request.post('/admin/student/baseinfo/importData', data, {timeout: 0});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

//获取导入数据进度
export async function importStudentBaseProgress(data) {
  const res = await request.get('/admin/student/baseinfo/importDataProgress/' + data);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 下载导入结果
 * @returns {Promise<void>}
 */
export async function downloadResult(params) {
  const res = await request({
    url: '/admin/student/baseinfo/exportUploadResult',
    method: 'POST',
    responseType: 'blob',
    data: toFormData(params),
  });
  download(res.data, `place_import_result_${Date.now()}.xlsx`);
}


/**
 * 通过老师工号或者姓名获取老师列表
 * @returns {Promise<any>}
 */
export async function listTeacherSearch(keyword) {
  const res = await request.get('/admin/student/baseinfo/search/teacher/' + keyword);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 设置学生带兵方案
 * @param params
 * @returns {Promise<*>}
 */
export async function setStudentLead(params) {
  const res = await request.put('/admin/student/baseinfo/setStudentLead', params);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 加密学生身份证号(只使用一次，不再使用)
 * @returns {Promise<*>}
 */
export async function encryptIdNumber() {
  const res = await request.put('/admin/student/baseinfo/encryptStudentIdNumber');
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}




